﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace MvcFriendlyModule
{
    public class StripViewStateStream : Stream
    {
        private Stream responseStream;

        StringBuilder sbBuffer;

        public StripViewStateStream(Stream stream)
        {
            responseStream = stream;

            sbBuffer = new StringBuilder();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string strBuffer = Encoding.UTF8.GetString(buffer, offset, count);

            if (strBuffer.IndexOf("</form>", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                strBuffer.IndexOf("</html>", StringComparison.InvariantCultureIgnoreCase) >= 0)
            {
                sbBuffer.Append(strBuffer);

                byte[] data = Encoding.UTF8.GetBytes(StripViewState(sbBuffer.ToString()));

                responseStream.Write(data, 0, data.Length);

                sbBuffer.Length = 0;
            }
            else
            {
                sbBuffer.Append(strBuffer);

            }
        }

        private string StripViewState(string strHTML)
        {
            string matchString1 = "type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\"";
            string matchString2 = "type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\"";
            string matchString3 = "type=\"hidden\" name=\"__EVENTTARGET\" id=\"__EVENTTARGET\"";
            string matchString4 = "type=\"hidden\" name=\"__EVENTARGUMENT\" id=\"__EVENTARGUMENT\"";

            string positiveLookahead1 = "(?=.*(" + Regex.Escape(matchString1) + "))";
            string positiveLookahead2 = "(?=.*(" + Regex.Escape(matchString2) + "))";
            string positiveLookahead3 = "(?=.*(" + Regex.Escape(matchString3) + "))";
            string positiveLookahead4 = "(?=.*(" + Regex.Escape(matchString4) + "))";

            RegexOptions opt = RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled;

            Regex[] arrRe = new Regex[] {
                new Regex("\\s*<div>" + positiveLookahead1 + "(.*?)</div>\\s*", opt),
                new Regex("\\s*<div>" + positiveLookahead2 + "(.*?)</div>\\s*", opt),
                new Regex("\\s*<div>" + positiveLookahead3 + "(.*?)</div>\\s*", opt),
                new Regex("\\s*<div>" + positiveLookahead3 + "(.*?)</div>\\s*", opt)
            };

            foreach (Regex re in arrRe)
            {
                strHTML = re.Replace(strHTML, "");
            }

            return strHTML;
        }

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override void Flush()
        {
            responseStream.Flush();
        }

        public override long Length
        {
            get { return responseStream.Length; }
        }

        long position;

        public override long Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return responseStream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return responseStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            responseStream.SetLength(value);
        }

        public override void Close()
        {
            responseStream.Close();
        }
    }
}
